import logging

logger = logging.getLogger(__name__)

class GlobalVariable(object):
    __instance = None

    def __new__(cls, *args, **kwargs):
        if not cls.__instance:
            cls.__instance = object.__new__(cls, *args)
        return cls.__instance

    def __init__(self):
        self.globalVars = {}
        self.res = []

    def getVar(self, key):
        '''获取某个全局变量'''
        return self.globalVars.get(key)

    def getVars(self):
        '''获取全部全局变量'''
        return self.globalVars

    def deleteVar(self, key):
        '''删除某个全局变量'''
        self.globalVars.pop(key)

    def cleanVars(self):
        '''清空全局变量'''
        self.globalVars.clear()

    def deleteVars(self):
        '''删除全局变量'''
        del self.globalVars

    def set_var(self, key, value):
        '''设置全局变量'''
        self.globalVars[key] = value
        logger.info(f"设置全局变量: {key} = {value}")

    def substitute_vars(self, text):
        '''替换文本中的全局变量'''
        from utils.substitution_tool import var_substitution  # 延迟导入
        return var_substitution(text, self.globalVars)

# 实例化并返回的单例对象
gv = GlobalVariable()
